package server;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;

/**
 * Created by IntelliJ IDEA.
 * User: Hp
 * Date: 25.05.11
 * Time: 1:11
 * To change this template use File | Settings | File Templates.
 */
public class ServerFrame extends JFrame {
    private boolean working=false;
    private JButton apply, repaint;
    private JLabel portLabel;
    private JTextField portField;
    private ReadOnlyView viewer;
    private GridBagConstraints gbc;
    static Logger logger=Logger.getLogger("global");
    private JTextArea log;
    private JScrollPane logPane;
    private ServerModel server;
    private ServerTCP serverTCP;

    public static void main(String[] args){
      ServerFrame frame=new ServerFrame();
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
    public ServerFrame(){
        setTitle("HTTP server");
        apply = new JButton("Apply");
        portLabel = new JLabel("Port number");
        portField = new JTextField("Port");
        setSize(400,100);
        apply.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if(working){
                apply.setText("Apply");
                serverTCP.stop();
                    working=false;
                    portField.setEnabled(true);
                    logger.log(Level.INFO,"Server stopped");
                    return;
                }
                if(check()){
                    server = new ServerModel(300, 300);
                    serverTCP = new ServerTCP();
                serverTCP.setServerModel(server);
                server.setServerInterface(serverTCP);
                serverTCP.start();
                    apply.setText("Stop");
                    working=true;
                    viewer=new ReadOnlyView();
                    viewer.setModel(server.pModel);
                    gbc.weightx=5;
                    gbc.gridx=0;
                    gbc.gridy=2;
                    gbc.weighty=5;
                    add(viewer,gbc);
                    repaint=new JButton("Refresh image");
                    repaint.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                            synchronized(server){
                                viewer.repaint();
                            }
                        }
                    });
                    gbc.gridx=6;
                    gbc.gridy=1;
                    add(repaint, gbc);
                    portField.setEnabled(false);
                    if (log == null) {
                        log = new JTextArea("Server log\n");
                        logPane=new JScrollPane(log);
                        logger.addHandler(new Handler() {
                            @Override
                            public void publish(LogRecord record) {
                                SimpleDateFormat format = new SimpleDateFormat("hh:mm:ss ");
                                log.append(format.format(Calendar.getInstance().getTime()) + record.getMessage()+"\n");
                            }

                            @Override
                            public void flush() {
                                //To change body of implemented methods use File | Settings | File Templates.
                            }

                            @Override
                            public void close() throws SecurityException {
                                //To change body of implemented methods use File | Settings | File Templates.
                            }
                        });
                        gbc.weightx = 5;
                        gbc.gridx = 7;
                        gbc.gridy = 2;
                        gbc.weighty = 4;
                        gbc.fill=GridBagConstraints.BOTH;
                        add(logPane, gbc);
                    }
                    log.getGraphics().setColor(Color.GREEN);
                    SimpleDateFormat format = new SimpleDateFormat("hh:mm:ss ");
                    log.append(format.format(Calendar.getInstance().getTime()) + " Server started\n");

                    setSize(600, 400);
                    repaint();
                }
            }
        });
        GridBagLayout layout=new GridBagLayout();
        gbc = new GridBagConstraints();
        setLayout(layout);
        gbc.fill=GridBagConstraints.HORIZONTAL;
        gbc.weightx = 1.0;
        gbc.weighty=1.0;
        gbc.gridx=0;
        gbc.gridy=0;
        gbc.gridwidth=1;
        add(portLabel, gbc);
        gbc.gridx=1;
        gbc.gridy=0;
        gbc.gridwidth=1;
        gbc.gridheight=1;
        add(portField, gbc);
        gbc.gridx=5;
        gbc.gridy=0;
        gbc.gridwidth=3;
        add(apply, gbc);

    }
    boolean check(){
        try {
            int port=Integer.parseInt(portField.getText());
            if (port<1000 ||port>65535) throw new NumberFormatException();
            ServerConstants.port=port;
        } catch (NumberFormatException e) {
            return false;
        }
        return true;
    }
}
